package com.cxso.example.sdk.domain;

import java.beans.Transient;
import java.io.Serializable;

public class Result<T> implements Serializable {

    private Integer code;

    private String msg;

    private T data;

    private Long total;

    private String requestId;

    public Result() {

    }

    private Result(Integer code, String msg, T data, Long total) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.total = total;
    }

    public static <T> Result<T> fail(String msg) {
        return new Result<>(500, msg, null, null);
    }

    @Transient
    public boolean isSuccess() {
        if (this.code == null) {
            return false;
        } else {
            return this.code == 200;
        }
    }

    @Transient
    public boolean isFail() {
        return !isSuccess();
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public Long getTotal() {
        return total;
    }

    public Result<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Result<T> setTotal(Long total) {
        this.total = total;
        return this;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }
}


